﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BCDto.Common.GeneratePassCode;
using BCDto.Common.GenerateReceivingCode;
using BCDto.Common.ValidatePassCode;
using BCDto.Common.ValidateReceivingCode;
using BCService.SmsService;
using Microsoft.AspNetCore.Mvc;

namespace BCTMSRESTful.Controllers
{
    /// <summary>
    /// 短信控制器
    /// </summary>
    [Route("api/v1/[controller]")]
    public class SmsController : BCTMSRESTfulAPIController
    {
        private readonly IAliyunSmsService aliyunSmsService;
        /// <summary>
        /// 短信控制器构造函数
        /// </summary>
        /// <param name="aliyunSmsService">短信处理服务接口</param>
        public SmsController(IAliyunSmsService aliyunSmsService)
        {
            this.aliyunSmsService = aliyunSmsService;
        }

        /// <summary>
        /// 生成新手机验证码。
        /// </summary>
        /// <param name="dto">手机验证码对象的实例。</param>
        /// <response code="200">已生成的手机验证码信息。</response>
        /// <returns></returns>
        [HttpPost("[action]")]
        [ProducesResponseType(typeof(GeneratePassCodeDto), 200)]
        public IActionResult GeneratePassCode([FromBody] GeneratePassCodeRequestDto dto)
        {
            return this.Ok(this.aliyunSmsService.SendRegisterPassCode(dto.Phone));
        }

        /// <summary>
        /// 验证手机验证码。
        /// </summary>
        /// <param name="dto">手机验证码对象的实例。</param>
        /// <response code="200">验证结果。</response>
        /// <returns></returns>
        [HttpPost("[action]")]
        [ProducesResponseType(typeof(ValidatePassCodeDto), 200)]
        public IActionResult ValidatePassCode([FromBody] ValidatePassCodeRequestDto dto)
        {
            if (!this.aliyunSmsService.ValidatePassCode(dto.Phone, dto.PassCode))
            {
                throw new ArgumentException("验证码验证失败。");
            }

            return this.Ok(new ValidatePassCodeDto { Result = "Success" });
        }

        /// <summary>
        /// 生成新的收货码。
        /// </summary>
        /// <param name="dto">手机验证码对象的实例。</param>
        /// <response code="200">已生成的手机验证码信息。</response>
        /// <returns></returns>
        [HttpPost("[action]")]
        [ProducesResponseType(typeof(GeneratePassCodeDto), 200)]
        public IActionResult GenerateReceivingCode([FromBody] GenerateReceivingCodeRequestDto dto)
        {
            return this.Ok(this.aliyunSmsService.SendReceivingCode(dto.Phone));
        }

        /// <summary>
        /// 验证收货码。
        /// </summary>
        /// <param name="dto">收货码对象的实例。</param>
        /// <response code="200">验证结果。</response>
        /// <returns></returns>
        [HttpPost("[action]")]
        [ProducesResponseType(typeof(string), 200)]
        public IActionResult ValidateReceivingCode([FromBody] ValidateReceivingCodeRequestDto dto)
        {
            if (!this.aliyunSmsService.ValidateReceivingCode(dto.Phone, dto.ReceivingCode))
            {
                throw new ArgumentException("收货码验证失败。");
            }

            return this.Ok("Success");
        }
    }
}
